package com.meadvc.ssmdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.dao.ComponentDao;
import com.meadvc.ssmdemo.dao.ContactsDao;
import com.meadvc.ssmdemo.service.ContactService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: ssm-demo
 * @description:
 * @author: meadvc
 * @create: 2020-06-21 08:45
 **/
@Service
public class ContactServiceImpl implements ContactService {
    @Resource
    private ContactsDao dao;
    /**
     * 展示联系人列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public JSONObject listContacts(String query, int pagenum, int pagesize) {
        List<JSONObject> list = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int offset = (pagenum-1)*pagesize;
        int total = 0;
        if(query==null||query.isEmpty()){
            list = dao.getContacts(offset,pagesize);
            total = dao.countConts();

        }else {
            offset = 0;
            list = dao.getContactsByName(query,offset,pagesize);
            pagenum =1;
            total = dao.countContsByName(query);
        }
        if(list!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }

        data.put("total",total);
        data.put("pagenum",pagenum);
        data.put("conts",list);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    /**
     * 添加联系人
     * @param requestJson
     * @return
     */
    @Override
    public JSONObject addContact(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.addContact(requestJson);
        if(index!=0){
            meta.put("msg","联系人添加成功");
            meta.put("status",200);
        }else{
            meta.put("msg","联系人添加失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    /**
     * 通过id获取联系人
     * @param tid
     * @return
     */
    @Override
    public JSONObject getContact(int tid) {
        JSONObject json = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        json = dao.getContactById(tid);
        if(json!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }
        data.put("contact",json);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject updateContact(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.updateContact(requestJson);
        if(index!=0){
            meta.put("msg","联系人修改成功");
            meta.put("status",200);
        }else{
            meta.put("msg","联系人修改失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject deleteContact(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.removeContact(requestJson);
        if(index!=0){
            meta.put("msg","联系人删除成功");
            meta.put("status",200);
        }else{
            meta.put("msg","联系人删除失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }
}
